package interficie;


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;

/**
 * 	Esta clase permite renderizar y editar un boton para poderlo usar como elemento
 * 	de una columna de una tabla
 *
 * @author Anna Lopez Mora
 */
public class ButtonRender extends AbstractCellEditor
	implements TableCellRenderer, TableCellEditor, ActionListener, MouseListener
{
	private JTable table;
	private Action action;
	private int mnemonic;
	private Border originalBorder;
	private Border focusBorder;

	private JButton renderButton;
	private JButton editButton;
	private Object editorValue;
	private boolean isButtonColumnEditor;

	/**
	 *  Crea el boton de columna para usarlo como renderizador.
	 *  Una vez creado se anadira directamente en la columna especificada.
	 *
	 *  @param table 	la tabla que contiene el boton
	 *  @param action 	la accion que desarrollara el boton al hacer clic
	 *  @param column 	la columna donde se coloca el boton
	 */
	public ButtonRender(JTable table, Action action, int column)
	{
		this.table = table;
		this.action = action;

		renderButton = new JButton();
		editButton = new JButton();
		editButton.setFocusPainted( false );
		editButton.addActionListener( this );
		editButton.setIcon(new ImageIcon(Principal.class.getResource("/resources/eliminar.png")));
		originalBorder = editButton.getBorder();
		setFocusBorder( new LineBorder(Color.BLUE) );

		TableColumnModel columnModel = table.getColumnModel();
		columnModel.getColumn(column).setCellRenderer( this );
		columnModel.getColumn(column).setCellEditor( this );
		table.addMouseListener( this );
	}



	/**
	 *  Efecto de color cuando apretamos el boton
	 *
	 *  @param focusBorder	elemento Border con las caracteristicas del boton. 
	 */
	public void setFocusBorder(Border focusBorder)
	{
		this.focusBorder = focusBorder;
		editButton.setBorder( focusBorder );
	}

	/**
	 * Las clases a continuacion forman parte directa de los elementos que esta clase implementa.
	 * 
	 * 
	 */
	@Override
	public Component getTableCellEditorComponent(
		JTable table, Object value, boolean isSelected, int row, int column)
	{
		if (value == null)
		{
			editButton.setIcon(new ImageIcon(Principal.class.getResource("/resources/eliminar.png")));
		}
		else if (value instanceof Icon)
		{
			editButton.setIcon(new ImageIcon(Principal.class.getResource("/resources/eliminar.png")));
		}
		else
		{
			editButton.setIcon(new ImageIcon(Principal.class.getResource("/resources/eliminar.png")));
		}

		this.editorValue = value;
		return editButton;
	}

	@Override
	public Object getCellEditorValue()
	{
		return editorValue;
	}
	public Component getTableCellRendererComponent(
		JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
	{
		if (isSelected)
		{
			renderButton.setForeground(table.getSelectionForeground());
	 		renderButton.setBackground(table.getSelectionBackground());
		}
		else
		{
			renderButton.setForeground(table.getForeground());
			renderButton.setBackground(UIManager.getColor("Button.background"));
		}

		if (hasFocus)
		{
			renderButton.setBorder( focusBorder );
		}
		else
		{
			renderButton.setBorder( originalBorder );
		}

		if (value == null)
		{
			renderButton.setIcon(new ImageIcon(Principal.class.getResource("/resources/eliminar.png")));
		}
		else if (value instanceof Icon)
		{
			renderButton.setIcon(new ImageIcon(Principal.class.getResource("/resources/eliminar.png")));
		}
		else
		{
			renderButton.setIcon(new ImageIcon(Principal.class.getResource("/resources/eliminar.png")));
		}

		return renderButton;
	}

	public void actionPerformed(ActionEvent e)
	{
		int row = table.convertRowIndexToModel( table.getEditingRow() );
		fireEditingStopped();


		ActionEvent event = new ActionEvent(
			table,
			ActionEvent.ACTION_PERFORMED,
			"" + row);
		action.actionPerformed(event);
	}

    public void mousePressed(MouseEvent e)
    {
    	if (table.isEditing()
		&&  table.getCellEditor() == this)
			isButtonColumnEditor = true;
    }

    public void mouseReleased(MouseEvent e)
    {
    	if (isButtonColumnEditor
    	&&  table.isEditing())
    		table.getCellEditor().stopCellEditing();

		isButtonColumnEditor = false;
    }

    public void mouseClicked(MouseEvent e) {}
	public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
}